﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FlanProject.Combats.Actions
{
    public class MeleeCombatAction : CombatAction
    {
        #region State

        public override bool IsOffensive { get { return true; } }
        public override bool IsTargetNeeded { get { return true; } }

        #endregion

        #region Advancing/Returning Data

        private static readonly int MOVE_LEFT = -1;
        private static readonly int MOVE_RIGHT = 1;
        private static readonly Vector2 advanceOffset = new Vector2(25f, 0f);
        private Vector2 direction;

        #endregion

        public MeleeCombatAction(CombatCharacter character) : base(character)
        {

        }

        #region Combat Stage

        protected override void StartStage()
        {
            switch (Stage)
            {
                case CombatActionStage.Preparing:
                {
                    Character.FaceTarget(Target.Position);
                    Character.CurrentState = CombatCharacter.States.Idle;
                    Character.OriginalPosition = Character.Position;
                    break;
                }

                case CombatActionStage.Advancing:
                {
                    Character.CurrentState = CombatCharacter.States.Walking;
                    break;
                }

                case CombatActionStage.Executing:
                {
                    Character.CurrentState = CombatCharacter.States.Attacking;
                    break;
                }

                case CombatActionStage.Returning:
                {
                    // Side switching
                    Character.FaceTarget(Character.OriginalPosition);
                    Character.CurrentState = CombatCharacter.States.Walking;
                    break;
                }

                case CombatActionStage.Finishing:
                {
                    Character.CurrentState = CombatCharacter.States.Idle;
                    break;
                }

                case CombatActionStage.Complete:
                {
                    Character.FaceTarget(Target.Position);
                    Character.CurrentState = CombatCharacter.States.Idle;
                    break;
                }
            }
        }

        protected override void UpdateCurrentStage(GameTime gameTime)
        {
            switch (Stage)
            {
                case CombatActionStage.Advancing:
                {
                    if (Character.CurrentState != CombatCharacter.States.Hit)
                    {
                        direction.X = Character.Position.X > Target.Position.X ? MOVE_LEFT : MOVE_RIGHT;

                        Character.PreviousPosition = Character.Position;
                        Character.Position += direction * Character.Stats.MovementSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }

                    Character.CurrentState = CombatCharacter.States.Walking;

                    break;
                }

                case CombatActionStage.Returning:
                {
                    if (Character.CurrentState != CombatCharacter.States.Hit)
                    {
                        direction.X = Character.Position.X < Target.Position.X ? MOVE_LEFT : MOVE_RIGHT;

                        Character.PreviousPosition = Character.Position;
                        Character.Position += direction * Character.Stats.MovementSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }

                    Character.CurrentState = CombatCharacter.States.Walking;

                    break;
                }
            }
        }

        protected override bool IsReadyForNextStage
        {
            get
            {
                switch (Stage)
                {
                    case CombatActionStage.Preparing:
                        return true;

                    case CombatActionStage.Advancing:
                        return Character.Position.X < Target.Position.X ?
                            Character.Position.X > Target.Position.X - advanceOffset.X ? true : false
                                : Character.Position.X < Target.Position.X + advanceOffset.X ? true : false;

                    case CombatActionStage.Executing:
                        return Character.CurrentAnimation.IsAnimationComplete || Character.CurrentState == CombatCharacter.States.Idle;

                    case CombatActionStage.Returning:
                        return direction.X == MOVE_LEFT ? Character.Position.X < Character.OriginalPosition.X
                            : Character.Position.X > Character.OriginalPosition.X;

                    case CombatActionStage.Finishing:
                        return true;
                }

                return base.IsReadyForNextStage;
            }
        }

        #endregion
    }
}
